package com.micro.common.biz;


import org.apache.commons.lang.RandomStringUtils;

import java.text.SimpleDateFormat;
import java.util.Collections;
import java.util.Date;
import java.util.List;
import java.util.Random;
import java.util.stream.Collectors;

/**
 * @desc 编号生成工具类
 * @author LevelCoder
 * @version V1.0.0
 * @email LevelCoder@126.com
 * @date 2019/6/25
 */
public class GeneratorCodeUtil {

    /** 设置邀请码默认生成长度为8位,减少重复邀请码,后续参数可改为自定义长度 */
    private static final int INVITATION_CODE_LENGHT = 8;

    /** 设置字符源 随机码生成时所需要基础依据 */
    private static final String GENERATE_SOURCE = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";

    /** 设置自增序列 */
    private static int sequence = 0;

    /** 设置补位长度位6 */
    private static int length = 6;

    /** 生成店铺编号 4位固定码 */
    public static String merchantHead = "1688";

    /** 生成用户编号 2位固定码*/
    public static String systemHead = "90";

    public static String customerHead = "393";

    /**
     * 生成用户编号/角色编号/菜单编号/部门编号/岗位编号等系统编号
     * @return
     */
    public static String generatorSystemCode(){
        String date = new SimpleDateFormat("yyMMdd").format(new Date());
        char[] bases = new char[]{'1','2','3','4','5','6','7','8','9'};
        List<Character> chars = date.chars().mapToObj(e -> (char)e).collect(Collectors.toList());
        Collections.shuffle(chars);
        String mid6 = chars.stream().map(Object ::toString).collect(Collectors.joining());
        String suffix8 = RandomStringUtils.random(1,bases)+RandomStringUtils.randomNumeric(7);
        return systemHead+mid6 + suffix8;
    }

    /**
     * 生成普通用户/会员用户/商户等编号
     * @return
     */
    public static String generatorCustomerCode(){
        String date = new SimpleDateFormat("yyMMdd").format(new Date());
        char[] bases = new char[]{'1','2','3','4','5','6','7','8','9'};
        List<Character> chars = date.chars().mapToObj(e -> (char)e).collect(Collectors.toList());
        Collections.shuffle(chars);
        String mid6 = chars.stream().map(Object ::toString).collect(Collectors.joining());
        String suffix8 = RandomStringUtils.random(1,bases)+RandomStringUtils.randomNumeric(7);
        return customerHead + mid6+suffix8;
    }

    /**
     * 生成账户编号
     * @return
     */
    public static String generatorAccountCode(String accountPrefix){
        String date = new SimpleDateFormat("yyMMdd").format(new Date());
        char[] bases = new char[]{'1','2','3','4','5','6','7','8','9'};
        List<Character> chars = date.chars().mapToObj(e -> (char)e).collect(Collectors.toList());
        Collections.shuffle(chars);
        String mid6 = chars.stream().map(Object ::toString).collect(Collectors.joining());
        String suffix8 = RandomStringUtils.random(1,bases)+RandomStringUtils.randomNumeric(7);
        return accountPrefix+mid6 + suffix8;
    }



    /**
     * 生成随机盐
     * @return
     */
    public static String generatorSalt(){
        int length = 12;
        Random random = new Random();
        StringBuffer sb = new StringBuffer();
        for(int i = 0; i < length; ++i){
            int number = random.nextInt(62);
            sb.append(GENERATE_SOURCE.charAt(number));
        }
        return sb.toString();
    }


    /**
     * 得到32位随机字符串
     * @param
     * @return
     */
    public static String getRandomString(){
        int length = 32;
        Random random = new Random();
        StringBuffer sb = new StringBuffer();
        for(int i = 0; i < length; ++i){
            int number = random.nextInt(62);
            sb.append(GENERATE_SOURCE.charAt(number));
        }
        return sb.toString();
    }



    /**
     * 获取随机数 -数字符号组号
     * @param length  表示生成字符串的长度
     * @return
     */
    public static String getRandomString(int length) {
        Random random = new Random();
        StringBuffer sb = new StringBuffer();
        for (int i = 0; i < length; i++) {
            int number = random.nextInt(GENERATE_SOURCE.length());
            sb.append(GENERATE_SOURCE.charAt(number));
        }
        return sb.toString();
    }


    /**
     * 获取随机数-纯数字
     * @param length 生成随机数长度
     * @return
     */
    public static String getRandomNumber(int length){
        String base = "0123456789";
        Random random = new Random();
        StringBuffer sb = new StringBuffer();
        for (int i = 0; i < length; i++) {
            int number = random.nextInt(base.length());
            sb.append(base.charAt(number));
        }
        return sb.toString();

    }

    /**
     * 生成8位邀请码
     * @return
     */
    public static String generatorInvitationCode(){
        String invitationCode = "";
        for(int i = 0 ; i < INVITATION_CODE_LENGHT ; i++){
            invitationCode += GENERATE_SOURCE.charAt((int) Math.floor(Math.random() * GENERATE_SOURCE.length()));
        }
        return  invitationCode;
    }


    /**
     * <h2>生成优惠券模版编号</h2>
     * @return
     * @param productLine
     * @param category
     */
    public static String generatorCouponTemplateCode(String productLine, String category){
        String date = new SimpleDateFormat("yyyyMMddHHmmss").format(new Date());
        char[] bases = new char[]{'1','2','3','4','5','6','7','8','9'};
        List<Character> chars = date.chars().mapToObj(e -> (char)e).collect(Collectors.toList());
        Collections.shuffle(chars);
        String time = chars.stream().map(Object ::toString).collect(Collectors.joining());
        String suffix8 = RandomStringUtils.random(1,bases)+RandomStringUtils.randomNumeric(7);
        return productLine+category+time + suffix8;
    }

    /**
     * 左侧补0
     * @param s
     * @param length
     * @return
     */
    public static String addLeftZero(String s, int length) {
        int old = s.length();
        if (length > old) {
            char[] c = new char[length];
            char[] x = s.toCharArray();
            if (x.length > length) {
                throw new IllegalArgumentException( "Numeric value is larger than intended length: " + s + " LEN " + length);
            }
            int lim = c.length - x.length;
            for (int i = 0; i < lim; i++) {
                c[i] = '0';
            }
            System.arraycopy(x, 0, c, lim, x.length);
            return new String(c);
        }
        return s.substring(0, length);

    }


    /**
     * <h2>生成spu编号</h2>
     * @return
     */
    public static String generatorSpuCode() {
        String date = new SimpleDateFormat("yyyyMMddHHmmss").format(new Date());
        char[] bases = new char[]{'1','2','3','4','5','6','7','8','9'};
        List<Character> chars = date.chars().mapToObj(e -> (char)e).collect(Collectors.toList());
        Collections.shuffle(chars);
        String time = chars.stream().map(Object ::toString).collect(Collectors.joining());
        String suffix8 = RandomStringUtils.random(1,bases)+RandomStringUtils.randomNumeric(7);
        return time + suffix8;
    }
}
